#include ("/_includes/_layout.html")
#@layout("添加规格", "wxmall,点步科技", "Wxmall社区","wxmall") 
#define content()
<div class="wrapper wrapper-content">
	<div class="ibox-content">
		<!-- <div class="ibox-title">
			<small><a href="#(webctx)/specification">《返回列表</a></small>
		</div> -->
		<div >
			<div class="contain-fluid" > 
				<form id="qiandaoForm" class="form-horizontal">
				  <div class="form-group">
			            <label class="col-md-2 control-label"><span class="requirespan">*</span>规格名称</label>
			            <div class="col-md-2" id="error_specification_name">
			            	<input type="hidden" id="specification_id" value="#(specification.id??)" class="input-xfat"  name="id"  >
			                <input id="specification_name" name="name" value="#(specification.name??)" maxlength="10" type="text" class="form-control"  >
			                <label class="control-label" for="specification_name"></label>
			            </div>
			        </div>
			        
			        <div class="form-group">
			            <label class="col-md-2 control-label">备注</label>
			            <div class="col-md-4" id="error_specification_memo">
			                <input id="specification_memo" name="memo" value="#(specification.memo??)" maxlength="20" type="text" class="form-control">
			                <label class="control-label" for="specification_memo"></label>
			            </div>
			        </div>
			        
			        <div class="form-group">
			            <label class="col-md-2 control-label">排序</label>
			            <div class="col-md-2" id="error_specification_orders">
			                <input id="specification_orders" name="orders" value="#(specification.orders??)" maxlength="20" type="text" class="form-control"  onkeyup="this.value=this.value.replace(/\D/g,'')">
			                <label class="control-label" for="specification_orders"></label>
			            </div>
			        </div>
			        
			        <div class="form-group">
			            <label class="col-md-2 control-label">状态</label>
			            <div class="col-md-2" id="error_active">
			                <select id="active_sel" class="form-control" style="width: 120px;">
			                	<option value="0" #if(specification && specification.active ==0) selected="selected" #end>删除</option>
			                	<option value="1" #if(specification== null || specification.active ==1) selected="selected" #end>启用</option>
			                </select>
			            </div>
			        </div>
			        
			         <div class="form-group">
			            <label class="col-md-2 control-label"> <span class="requirespan">*</span> 添加规格值 </label>
			            <div class="col-md-8 active_statistics" id="error_specification_value">
			                 <div class="sign_activity sign_activity_tabin" id="sign_type_set_join">
		                        <ul class="sign_activity_list" id="prize_list_join">
		                        #for(value: specifitionValue)
		                        		<li #if(value.specificationId) class="self" #else class="system" #end item-id="#(value.id)">
			                              	<p class="fl">
			                                  	规格值： <input maxLength="20" value="#(value.name)" class="input-xfat" style="width: 100px;" name="specificationValue_name"  type="text">
			                                  	排序：<input maxLength="8" value="#(value.orders)" class="input-xfat" style="width: 100px;" name="specificationValue_orders"  type="text" onkeyup="this.value=this.value.replace(/\D/g,'')"> 　
			                              	#if(value.specificationId)
				                            	<a class="but_remove" href="javascript:void(0);" onclick="deleteSpecValue(this);">×</a>
				                            #end
			                              	</p>
			                              	<input type="hidden" value="#(value.id)" name="specificationValue_id"  >
			                            </li>
			                     #end
		                        </ul>
		                        <p>
		                            <a href="javascript:void(0);" onclick="addSpecValue('join');" class="btn-bordered btn-xlarge btn-primary">添加规格值</a>
		                            <label id="join_error_msg" style="color:red;"></label>
		                        </p>
			            </div>
			        </div>
			        </div>
				</form>
			</div>
		</div>
		<div class="panel-footer" align="center">
		   	 <button id="saveSpeci" onclick="saveSpeci();" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-floppy-disk"></span> 保 存</button>
		   	 <a href="#(webctx)/specification" class="btn btn-default btn-sm">取   消</a>
		</div>
	</div>
		
</div>

  <textarea style="display:none;" id="html_model_join">                            
  	  <li class="self">
       <p class="fl">
            	规格值： <input maxLength="20" class="input-xfat" style="width: 100px;" name="specificationValue_name"  type="text">
            	 排序：<input maxLength="8" class="input-xfat" style="width: 100px;" name="specificationValue_orders"  type="text" onkeyup="this.value=this.value.replace(/\D/g,'')">　
     	<a class="but_remove" href="javascript:void(0);" onclick="deleteSpecValue(this);">×</a>
    </p>
    <input type="hidden" value="" name="specificationValue_id"  >
     </li>
  </textarea>
<script type="text/javascript">
function addSpecValue(type){
	$("#prize_list_"+type).append($("#html_model_"+type).val());
}
function deleteSpecValue(obj){
	var itemId = $(obj).parent().parent().attr("item-id");
	if(itemId && itemId != ""){
		$(obj).parent().parent().hide();
		$(obj).parent().parent().attr("del","del");
	}else{
		$(obj).parent().parent().remove();
	}
}

function saveSpeci(){
	var params = {},error = {};
	var specificationName = $("#specification_name").val(), specificationMemo= $("#specification_memo").val(), 
	specificationOrders = $("#specification_orders").val(),specificationId= $("#specification_id").val();
	if(specificationId!=null && specificationId!="") params.id = specificationId;
	if(specificationName!=null && specificationName!="") params.name = specificationName;
	params.active = $("#active_sel").val();
	params.memo = specificationMemo;
	params.orders=specificationOrders;
	if($.trim(specificationName) =="") {error.error_specification_name = "规格名称不能为空"; } else {error.error_specification_name=""; params.name = $.trim(specificationName);}
	var hasError = false;
	  for(var key in error){
		if(error[key]!=""){
			if(!hasError) hasError = true;
			$("#"+key).addClass("has-error");
			$("#"+key).find("label").text(error[key]);
		}else{
			$("#"+key).removeClass("has-error");
			$("#"+key).find("label").empty();
		}
	}
	if(hasError) return false;
	
	var hasErr = false;
	$("#sign_type_set_join li.self").each(function(i,obj){
		var specificationValue_name = $(obj).find("input[name='specificationValue_name']");
		if(specificationValue_name.val()==""){
			if(!hasErr) hasErr=true;
			$("#join_error_msg").html("规格值不能为空");
			specificationValue_name.css("border-color", "#ff5454");
			return ;
		}
		var specificationValue_orders = $(obj).find("input[name='specificationValue_orders']");
		if(specificationValue_orders.val()==""){
			if(!hasErr) hasErr=true;
			$("#join_error_msg").html("排序不能为空");
			specificationValue_orders.css("border-color", "#ff5454");
			return ;
		}
	});
	if(hasErr){
		return false;
	}
	var items = new Array();
	$("#sign_type_set_join li.self").each(function(i, obj){
		var specificationValue_name= $(obj).find("input[name='specificationValue_name']").val();
		var specificationValue_orders= $(obj).find("input[name='specificationValue_orders']").val();
		var specificationValue_id= $(obj).find("input[name='specificationValue_id']").val();
		var del = $(obj).attr("del");
		if(specificationValue_name!="" && specificationValue_orders!=""){
			var entity = new Object();
			entity.type = 1;
			entity.name = specificationValue_name;
			entity.orders=specificationValue_orders;
			if(specificationValue_id != ""){
				entity.itemId = specificationValue_id;
			}
			if(del == "del"){
				entity.del = del; 
			}
			items.push(entity);
		}
	});

	
	//排除del的规格值
	var itemsUnDels = new Array();
	for(var k=0;k<items.length;k++){
		var _objSpVaule = items[k];
		if(_objSpVaule.del != "del"){
			itemsUnDels.push(_objSpVaule);
		}
	}
	
	if(itemsUnDels.length <=0) {
		obz.warn("请填写规格值");
		return;
	}
	
	params.items =  JSON.stringify(items);
	$(".wrapper-content").mask("正在保存...");
	obz.ajaxJson(obz.ctx+"/specification/save", params, function(json) {
		$(".wrapper-content").unmask();
		if(json.code!=200){
			BootstrapDialog.alert({title:'提示', message:json.msg});
		}else{
			obz.msg("保存成功", function(){
				location.href=obz.ctx + "/specification/index";
			});
		}
	},"json");
}
</script>
#end